package modelo.persistencia;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class GenericoDAO<T> {
	private Session session;
	private Transaction transacao;
	
	public GenericoDAO(){		
	}
	
	//método responsável pelo login do sistema		
	@SuppressWarnings("unchecked")
	public T login(Class<?> c,String usuario, String senha){        
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();             
		Criteria criteria=this.session.createCriteria(c);
		criteria.add(Restrictions.eq("usuario", usuario));  
		criteria.add(Restrictions.eq("senha", senha));
		transacao.commit();            
		return (T) criteria.uniqueResult();         
	} 
	
	public T inserir(T obj){
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();
		session.save(obj);
		transacao.commit();
		session.close();
		return obj;
	}	
	
	public T inserir_editar(T obj){
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();
		session.saveOrUpdate(obj);
		transacao.commit();
		session.close();
		return obj;
	}	
	
	public void remover(T obj){
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();
		session.delete(obj);
		transacao.commit();	
		session.close();
	}
	
	public void editar(T obj){
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();
		session.update(obj);
		transacao.commit();	
		session.close();
	}
	
	public List<?> listar(Class<?> c){  
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();  
        List<?> objts;  
        objts = null;  
        Criteria selectAll = session.createCriteria(c);  
        transacao.commit();  
        objts = selectAll.list();  
        session.close();  
        return objts;  
    } 
	
	@SuppressWarnings("unchecked")
	public T consultarPorId(Class<?> c,int id) {
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();
	    T t=null;
	    t= (T)session.load(c, id);
	    t.toString();
	    session.close();
	    return t;
	}	 
	
	public List<?> listarPaginacao(String campo, String pesquisa, Class<?> c, int firstResult, int maxResult)  {
	     session=HibernateUtil.getInstance();
	     transacao= session.beginTransaction();
	     Criteria criteria = this.session.createCriteria(c);
	     criteria.add( Restrictions.like(campo.toLowerCase(), pesquisa, MatchMode.START));
	     criteria.setFirstResult(firstResult);
	     criteria.setMaxResults(maxResult);
	     transacao.commit();
	     return criteria.list();
	}
	
	//usado para listar os alunos da turma
	public List<?> listar_porID(Class<?> c, String campo, T obj)  
    {  
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();  
        List<?> objts;  
        objts = null;  
        Criteria selectAll = session.createCriteria(c);  
        selectAll.add(Restrictions.eq(campo, obj));  
        selectAll.addOrder( Order.asc("nome"));  
        transacao.commit();  
        objts = selectAll.list();  
        session.close();  
        return objts;  
    }
	
	//usado para listar professores ativos	
	public List<?> listar_Restricao(Class<?> c)  
    {  
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();  
        List<?> objts;  
        objts = null;  
        Criteria selectAll = session.createCriteria(c);  
        selectAll.add(Restrictions.eq("ministra_aula", true));  
        selectAll.addOrder( Order.asc("nome"));  
        transacao.commit();  
        objts = selectAll.list();  
        session.close();  
        return objts;  
    }
	
	//usado para listar com restricao	
		public List<?> listar_Restricao_generico(Class<?> c, String nome_campo, Object valor)  
	    {  
			session=HibernateUtil.getInstance();
			transacao=session.beginTransaction();  
	        List<?> objts;  
	        objts = null;  
	        Criteria selectAll = session.createCriteria(c);  
	        selectAll.add(Restrictions.eq(nome_campo, valor));
	        transacao.commit();  
	        objts = selectAll.list();  
	        session.close();  
	        return objts;  
	    }
	
	public void executaHQL(String hql){
		session=HibernateUtil.getInstance();
		transacao=session.beginTransaction();  		
		org.hibernate.Query q= session.createSQLQuery(hql);
		((org.hibernate.Query) q).executeUpdate();
		transacao.commit();
		session.close();
	}
	

}
